﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;

namespace Gameplay.PVE.Survivor
{
    public class StraightMovement : BulletMovement
    {
        private float moveTime;
        private Vector3 startPosition;
        private Vector3 targetPosition;
        private float startTime;
        private Vector3 lastPosition;
        
        public override void Start()
        {
            isFinish = false;
            startTime = TimeManager.time;
            startPosition = bullet.Position;
            lastPosition = startPosition;
            targetPosition = bullet.targetPosition;
            if (bullet.config.destroy_type == (int)EBulletDestroyType.OnTime)
            {
                moveTime = bullet.config.destroy_param;
                var direction = (targetPosition - startPosition);
                direction.y = 0;
                direction.Normalize();
                if (bullet.speed > 0)
                {
                    targetPosition = startPosition + direction * bullet.speed * moveTime;
                }
            }
            else if (bullet.config.destroy_type == (int)EBulletDestroyType.OnReachTarget)
            {
                var direction = (targetPosition - startPosition);
                direction.y = 0;
                direction.Normalize();
                targetPosition = startPosition + direction * bullet.config.max_distance;
                if (bullet.speed == 0)
                {
                    moveTime = 0;
                }
                else
                {
                    moveTime = Vector3.Distance(startPosition, targetPosition) / bullet.speed;
                }
            }
            else if (bullet.speed > 0)
            {
                moveTime = Vector3.Distance(startPosition, targetPosition) / bullet.speed;
            }
            else
            {
                moveTime = 0;
            }
        }
        
        public override void Update()
        {
            if (moveTime == 0)
            {
                bullet.Position = startPosition;
                bullet.Forward = targetPosition - startPosition;
                bullet.HitByCollision();
                isFinish = true;
            }
            else
            {
                float t = (TimeManager.time - startTime) / moveTime;
                if (bullet.config.speed == 0)
                {
                    bullet.Position = startPosition;
                }
                else
                {
                    bullet.Position = Vector3.Lerp(startPosition, targetPosition, t);
                }
                bullet.Forward = targetPosition - startPosition;
                bullet.HitByCollision();
                if (bullet.config.shape_type != 0 && lastPosition != bullet.Position)
                {
                    //还需要检查路径点,把路径想象成矩形
                    bullet.HitByTrail(lastPosition,bullet.Position);
                }
                lastPosition = bullet.Position;
                if (t >= 1)
                {
                    isFinish = true;
                }
            }
        }

        public override bool IsEnd()
        {
            return isFinish;
        }
    }
}
